/*
 * screen.h: simplify the usage of SDL2 (Simple DirectMedia Layer) library 
 */

#ifndef __SCREEN_H__
#define __SCREEN_H__

#include <SDL.h>
#include <SDL_ttf.h>

 /* short for types */
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned long long u64;
typedef char i8;
typedef short i16;
typedef int i32;
typedef long long i64;
typedef float f32;
typedef double f64;

/* bool type */
typedef SDL_bool bool;
#define false SDL_FALSE
#define true SDL_TRUE

/* font setting */
#define DEFAULT_PTSIZE  24

/* event types */
#define QUIT SDL_QUIT
#define KEYDOWN		SDL_KEYDOWN
#define KEY_UP		SDLK_UP
#define	KEY_DOWN	SDLK_DOWN 
#define KEY_LEFT	SDLK_LEFT
#define KEY_RIGHT	SDLK_RIGHT
#define KEY_a		SDLK_a
#define KEY_s		SDLK_s
#define KEY_d		SDLK_d
#define KEY_w		SDLK_w
#define KEY_SPACE	SDLK_SPACE
#define KEY_ESCAPE	SDLK_ESCAPE
#define KEY_RETURN	SDLK_RETURN
#define KEY_ENTER	SDLK_KP_ENTER

/* global var */
extern SDL_Window* window;		/* main window */
extern SDL_Surface* screen;		/* display surface */
extern SDL_Renderer* renderer;	/* canvas */

extern SDL_Event event;			/* handle events */

/* color is combined with rgb */
typedef struct {
    u8 r, g, b;
}RGB;

/* draw tools */
/* set color */
#define setcolor(r,g,b) SDL_SetRenderDrawColor(renderer, r, g, b, SDL_ALPHA_OPAQUE)
/* draw line with 2 points */
#define drawline(x1,y1,x2,y2) SDL_RenderDrawLine(renderer, x1, y1, x2, y2)
/* draw rectangle with position and width, height */
#define drawrect(left,top,width,height) {SDL_Rect _r = {left,top,width,height}; \
										 SDL_RenderDrawRect(renderer, &_r);}
/* draw and fill the rectangle */
#define fillrect(left,top,width,height) {SDL_Rect _r = {left,top,width,height}; \
										 SDL_RenderFillRect(renderer, &_r);}

/* event handler */
#define event_poll() SDL_PollEvent(&event)
#define event_type(t) (event.type == t)
#define event_key()	(event.key.keysym.sym)

/* get current time ticks */
#define getticks() SDL_GetTicks()

#define SCREEN_FPS 60
#define SCREEN_TICK_PER_FRAME (1000 / SCREEN_FPS)

/* description of these functions are in screen.c */
int screen_init(int w, int h);
void screen_quit(void);
void screen_setcaption(const char *str);
void screen_clear(int r, int g, int b, int a);

void settextcolor(int r, int g, int b); 
bool outtextxy(const char *str, int x, int y);

#endif /* __SCREEN_H__ */

